<?php

#chat.php
#
#Part of chatboss project
#
#POST the outputObject directly from the client , returns object.
#

include 'queryengine.php';
include 'functions.php';

#add actionids here
global $outputObject;
$outputObject = new protocolObject();

if(isset($_POST['inputObject']))
{
	$inputObject = json_decode((string)$_POST['inputObject']);
	$ouputObject->actionId = $inputObject->actionId;
	$outputObject->messageSender = $inputObject->messageSender;
	$outputObject->messageRecipient = $inputObject->messageRecipient;
	$outputObject->messageText = $inputObject->messageText;
	$outputObject->userPassword = $inputObject->userPassword;
	$outputObject->debugMode = $inputObject->debugMode;
	
	if(gettype($inputObject))
	{
		switch($inputObject->actionId)
		{
			case 1:
				sendMessage($inputObject);
				break;
			case 2:
				getMessage($inputObject);
				break;
			case 3:
				addUser($inputObject);
				break;
			case 4:
				delUser($inputObject);
				break;
			default:
				noAction($inputObject);
				break;
		}
		

		sendDebugInfo($outputObject);
		echo json_encode($outputObject);
	}
	else
	{
		$outputObject->giveExitStatus(1,"The input type is incorrect. Ensure you are using an object.");
		echo json_encode($outputObject);
	}

	
}
#error based on script input
else
{
	$outputObject->giveExitStatus(1,"This script only takes one object input");
	echo json_encode($outputObject);
}

#
#send messages to user
#
# Requires:
#
# actionId = 1
#
# messageSender
#	The userID of the user who sent the message
#
# messageRecipient
#	The userID of the message recipient
#
# messageText
#	The text of the message itself
#
function sendMessage($inputObject)
{
	#post code
	global $outputObject;
	
	$User_Send = new User();
	$User_Receive = new User();
	
	$User_Send->userName = $outputObject->messageSender;
	$User_Send->userPass = $outputObject->userPassword;
	$User_Send->checkUnique();
	
	$User_Receive->userName = $outputObject->messageRecipient;
	$User_Receive->checkUnique();
	
	if($User_Receive->isUnique == true && $User_Send->isUnique == true)
	{
		if($User_Send->checkPassword())
		{
			crowFather("insert into message(messageSender,messageRecipient,messageText) values(" . crowFather($User_Send->userName,6) . " , " . crowFather($User_Receive->userName,6) . " , " . crowFather($outputObject->messageText,6) . ");",3);
			$outputObject->giveExitStatus(0,"Message sent");
		}
		else
		{
			$outputObject->giveExitStatus(1,"Password was incorrect");
			$outputObject->User_SendData = $User_Send;
		}
	}
	else
	{
		$outputObject->giveExitStatus(1,"Users not unique");
	}
	
}

#
#Get a list of messages sent to a user
#
# Requires:
#
# actionId = 2
#
# messageRecipient
#	The userId of the user who received the messages.
#

function getMessage($outputObject)
{
	#get code
	global $outputObject;
	$messageQuery = new messageQuery();
	$outputObject->createTableQuery(array("Sender","Message"),array("Recipient",$outputObject->messageRecipient,"sentFlag","Y"),"queryMessageView");
	$messageQuery->getResultArray($outputObject);
	$messageQuery->getTable();
	$outputObject->returnData = $messageQuery->returnData;
	if($outputObject->debugMode == 1)
	{
		$outputObject->messageQueryProperties = $messageQuery;
	}
}

#
#Add a new user
#
# Requires:
#
# actionId = 3
#
# messageRecipient
#	The user you wish to add
#
# [userPrivilegeSet]
#		Default: 1
#	The user privileges you want to add to the account. Must correspond to a valid privilege set.
#
# [userDateOfBirth]
#		Default: Today's date
#	The user's date of birth. Will default to today's date and time if you don't set it
#
# [userDescription]
#	The user's brief description for the account. Not required at all.
#
function addUser($outputObject)
{
	#add user code
	global $outputObject;
	if($outputObject->messageRecipient == "" || $outputObject->messageRecipient == null)
	{
		$outputObject->giveExitStatus(1,"No user was provided");
	}
	else
	{
	
		$newUser = new User();
		$newUser->userName = $outputObject->messageRecipient;
		$newUser->userPass = $outputObject->userPassword;
		$newUser->addUser();
		$outputObject->exitCode = $newUser->exitCode;
		$outputObject->exitText = $newUser->exitText;
		
	}
	
}

#
#Delete an existing user
#
# Requires:
#
# actionId = 4
#
# messageSender
#	The user you wish to delete
#
function delUser($outputObject)
{
	#delete user code
	global $outputObject;
}

#user
#	Should be a user object
#
function userVeriy($user)
{
	if(isset($user->userName))
	{
		
	}
	else
	{
		$user->giveExitStatus(1,"userName was not specified");
	}
}

#An error checking function for object/input based errors
function noAction($outputObject)
{
	#error checking function for actionId based fail
	global $outputObject;
	if($outputObject->exitCode == 2)
	{
		#Exit status unchanged (nothing ran)
		$outputObject->giveExitStatus(1,"An exit status was not provided");
	}
	
	if($outputObject->actionId > 4)
	{
		$outputObject->giveExitStatus(1,"The action type was not one of the supported actions");
	}
}

?>